var ajax_div = {};
ajax_div.initialize_token = 0;
ajax_div.overlay_id = 'ajax-message-popup-window-mask';
ajax_div.loading_id = 'ajax-loading';
ajax_div.base_skin;
ajax_div.loading_w;
ajax_div.loading_h;
ajax_div.comfirmation_w;
ajax_div.comfirmation_h;
ajax_div.title;


ajax_div.zindex = 300;
ajax_div.wrapper = 'ajax-message-popup-window';
ajax_div.header = 'ajax-message-popup-head';
ajax_div.message = 'ajax-message-popup-message';
ajax_div.content = 'ajax-message-popup-content';
ajax_div.response_content = 'ajax-message-popup-content-response';

ajax_div.loading = function(){
	if( !$(ajax_div.loading_id) ){
		$j('#'+ajax_div.content).append("<div class=\""+ajax_div.loading_id+"\" id=\""+ajax_div.loading_id+"\" style=\"display: none;\"><img src=\""+ajax_div.base_skin+"/opc-ajax-loader.gif\"></div>");
		$j('#'+ajax_div.content).append("<div class=\""+ajax_div.response_content+"\" id=\""+ajax_div.response_content+"\"></div>");
	}
	
	$(ajax_div.wrapper).style.display = '';	
	$(ajax_div.loading_id).style.display = '';	
	
}

ajax_div.hideLoading = function(){
	if( $(ajax_div.loading_id) ){
		$(ajax_div.loading_id).style.display = '';	
	}	
}

ajax_div.overlay = function( display ){	
	if( !$(ajax_div.overlay_id) ){		
		$j('body').append("<div class=\""+ajax_div.overlay_id+"\" id=\""+ajax_div.overlay_id+"\" style=\"display: none;cursor: pointer;\">&nbsp;</div>");
	}
	
	if( display )
		$(ajax_div.overlay_id).style.display = '';
	else $(ajax_div.overlay_id).style.display = 'none';
	
}


ajax_div.init = function( box ){
	ajax_div.overlay( 1 );	
	if( !ajax_div.initialize_token ){
		if( !box ){
			//append overlay.
			$j('body').append("<div class=\""+ajax_div.wrapper+"\" id=\""+ajax_div.wrapper+"\" style=\"display: none;\"></div>");
			$j("#"+ajax_div.wrapper).append("<div class=\""+ajax_div.header+"\" id=\""+ajax_div.header+"\"><span class=\"title\">"+ajax_div.title+"</span><a class=\"close\" onclick=\"ajax_div.hide( ajax_div.wrapper );ajax_div.overlay(0);\"><img src=\""+ajax_div.base_skin+"/btn_window_close.gif\"></a></div>");
			$j("#"+ajax_div.wrapper).append("<div class=\""+ajax_div.message+"\" id=\""+ajax_div.message+"\"></div>");
			$j("#"+ajax_div.wrapper).append("<div class=\""+ajax_div.content+"\" id=\""+ajax_div.content+"\"></div>");
			ajax_div.initialize_token = 1;
			ajax_div.center(ajax_div.wrapper);
		}else{
			$j(box).append("<div class=\""+ajax_div.wrapper+"\" id=\""+ajax_div.wrapper+"\" style=\"display: none;\"></div>");
			$j("#"+ajax_div.wrapper).append("<div class=\""+ajax_div.message+"\" id=\""+ajax_div.message+"\"></div>");
			$j("#"+ajax_div.wrapper).append("<div class=\""+ajax_div.content+"\" id=\""+ajax_div.content+"\"></div>");
			ajax_div.initialize_token = 1;
			
		}
		
	}
	ajax_div.hide( ajax_div.message );
	
}

ajax_div.hide = function( div ){
	$(div).style.display = 'none';
	
}

ajax_div.showResponse = function( response ){
	ajax_div.hide( ajax_div.loading_id );
	if( $(ajax_div.overlay_id).style.display != 'none' ){
		if( response.responseText.isJSON() ){		
			var obj = response.responseText.evalJSON();
			if( ajax_div.message ){
				ajax_div.display( ajax_div.message );
				$(ajax_div.message).innerHTML = obj.message;
			}
			
			if( obj.message_level != 'error' ){			
				$(ajax_div.response_content).innerHTML = '';
			}
			
			if( obj.js != '' ){
				eval( obj.js );
			}
		}else{	
			$(ajax_div.response_content).innerHTML = '';
			$j('#'+ajax_div.response_content).append(response.responseText);
		}
		ajax_div.center(ajax_div.wrapper);
	}
}


ajax_div.div = function( id ){
	
	ajax_div.overlay(1);
	ajax_div.loading();	
	
	ajax_div.hide( ajax_div.loading_id );
	if( $(ajax_div.overlay_id).style.display != 'none' ){		
		ajax_div.display( ajax_div.message );
		$(ajax_div.response_content).innerHTML = '';
		$(ajax_div.response_content).innerHTML = $(id).innerHTML;
		ajax_div.center(ajax_div.wrapper);
	}
}

ajax_div.get = function( url ){
	url = ajax_div.cleanUrl(url);
	ajax_div.overlay(1);
	ajax_div.loading();
	
	//alert(url);
	var myAjax = new Ajax.Request(
	url,
	{
		method: 'post',
		postBody: '',
		onException: function (xhr, e)
		{
			alert('Exception : ' + e);
		},
		onComplete: function (xhr)
		{			
			ajax_div.showResponse(xhr);
		}

	});
}

ajax_div.submit = function( form ){
	var dataForm = new VarienForm(form, true);
	if(dataForm.validator && dataForm.validator.validate()){
		ajax_div.overlay(1);
		ajax_div.loading();
		
		url = ajax_div.cleanUrl($(form).action);
		var myAjax = new Ajax.Request(
		url,
		{
			method: 'post',
			postBody: $(form).serialize(),
			parameters : Form.serialize(form),
			onException: function (xhr, e)
			{
				alert('Exception : ' + e);
			},
			onComplete: function (xhr)
			{
				ajax_div.showResponse(xhr);
			}

		});
	}else{
		return false;
	}	
}



ajax_div.display = function( div ){
	$(div).style.display = '';
}


ajax_div.center = function (element) {	
    if($(element) != null) {

          // retrieve required dimensions
            var el = $(element);
            var elDims = el.getDimensions();
            var browserName=navigator.appName;
            if(browserName==="Microsoft Internet Explorer") {

                if(document.documentElement.clientWidth==0) {
                    //IE8 Quirks
                    //alert('In Quirks Mode!');
                    var y=(document.viewport.getScrollOffsets().top + (document.body.clientHeight - elDims.height) / 2);
                    var x=(document.viewport.getScrollOffsets().left + (document.body.clientWidth - elDims.width) / 2);
                }
                else {
                    var y=(document.viewport.getScrollOffsets().top + (document.documentElement.clientHeight - elDims.height) / 2);
                    var x=(document.viewport.getScrollOffsets().left + (document.documentElement.clientWidth - elDims.width) / 2);
                }
            }
            else {
                // calculate the center of the page using the browser andelement dimensions
                var y = Math.round(document.viewport.getScrollOffsets().top + ((window.innerHeight - $(element).getHeight()))/2);
                var x = Math.round(document.viewport.getScrollOffsets().left + ((window.innerWidth - $(element).getWidth()))/2);
            }
			
            // set the style of the element so it is centered
            var styles = {
                position: 'absolute',
                top: y + 'px',
                left : x + 'px'
            };
			
            el.setStyle(styles);			
			$j("#"+element).css( "z-index",ajax_div.zindex++ );
			
     }	 
}


ajax_div.centerWindow = function (element) {
	if( !$( ajax_div.conformation ) ){
		$j('body').append("<div class=\""+ajax_div.conformation+"\" id=\""+ajax_div.conformation+"\" style=\"display: none;\"><div class=\"tool-bar\"><a class=\"close\" onclick=\"ajax_div.hide( ajax_div.conformation );ajax_div.overlay(0);\"><img src=\""+ajax_div.base_skin+"/btn_window_close.gif\"></a></div><div class=\"confirmation_content\"></div></div>");
	}
	
	if($(element) != null) {
		$j("#"+ajax_div.conformation + " .confirmation_content").append( $j("#"+element).html() );		
	}
	
    if($(element) != null) {

          // retrieve required dimensions
            var el = $(element);
            var elDims = el.getDimensions();
            var browserName=navigator.appName;
            if(browserName==="Microsoft Internet Explorer") {

                if(document.documentElement.clientWidth==0) {
                    //IE8 Quirks
                    //alert('In Quirks Mode!');
                    var y=(document.viewport.getScrollOffsets().top + (document.body.clientHeight - elDims.height) / 2);
                    var x=(document.viewport.getScrollOffsets().left + (document.body.clientWidth - elDims.width) / 2);
                }
                else {
                    var y=(document.viewport.getScrollOffsets().top + (document.documentElement.clientHeight - elDims.height) / 2);
                    var x=(document.viewport.getScrollOffsets().left + (document.documentElement.clientWidth - elDims.width) / 2);
                }
            }
            else {
                // calculate the center of the page using the browser andelement dimensions
                var y = Math.round(document.viewport.getScrollOffsets().top + ((window.innerHeight - $(element).getHeight()))/2);
                var x = Math.round(document.viewport.getScrollOffsets().left + ((window.innerWidth - $(element).getWidth()))/2);
            }
			
            // set the style of the element so it is centered
            var styles = {
                position: 'absolute',
                top: y + 'px',
                left : x + 'px'
            };
			
            $( ajax_div.conformation ).setStyle(styles);			
			$j("#"+ajax_div.conformation).css( "z-index",ajax_div.zindex++ );
			
     }
	 
	ajax_div.display( ajax_div.conformation );
}

ajax_div.cleanUrl = function(url){
	var jsHost = (("https:" == document.location.protocol) ? "https://" : "http://");
	var search = (("https:" == document.location.protocol) ? "http://" : "https://");
	
	return url.replace( search,jsHost );
}
$j("#"+ajax_div.overlay_id).live('click',function(){    	
	ajax_div.hide( ajax_div.wrapper );ajax_div.overlay(0);
});
